package cn.hpclub.server.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.model.Video;
import cn.hpclub.server.service.VideoService;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.validator.admin.VideoValidator;

public class VideoController extends BaseAdminController<Video>{
    public static final String  HTML_INPUT = "/admin/video_input.tpl";
    public static final String  HTML_LIST  = "/admin/video_list.tpl";
    private static final String VIDEO_ID   = "id";

    public void list(){
        ConcurrentHashMap<String, Object> searchInfo = getSearchFilter();

        findByPageForSearch(VideoService.service.getSelect(), VideoService.service.getSqlFilter(searchInfo));
        setAttrs(searchInfo);

        render(HTML_LIST);
    }

    /**
     * @Title: getSearchFilter
     * @Description:获取查询条件
     * @return ConcurrentHashMap<String,String>
     */
    private ConcurrentHashMap<String, Object> getSearchFilter(){
        ConcurrentHashMap<String, Object> searchInfo = getObjectSearchInfo();

        return searchInfo;
    }

    public void add(){
        // setAttr(VIDEO_ID, 0);
        render(HTML_INPUT);

    }

    /**
     * @Title: edit
     * @Description: 编辑
     */
    public void edit(){

        String id = getPara(VIDEO_ID, "");
        Video v = Video.dao.findById(id);

        setAttr("id", v.getInt(Video.ID));
        setAttr("name", v.getStr(Video.NAME));
        setAttr("url", v.getStr(Video.URL));
        setAttr("detail", v.getStr(Video.DETAIL));
        setAttr("remark", v.getStr(Video.REMARK));
        setAttr("sort", v.getInt(Video.SORT));
        setAttr("enabled", v.getInt(Video.ENABLED));

        render(HTML_INPUT);
    }

    /**
     * @Title: save
     * @Description:新增保存
     */
    @Before({ Tx.class, VideoValidator.class })
    public void save(){
        setVideo();
    }

    /**
     * @Title: update
     * @Description:编辑保存
     */
    @Before({ Tx.class, VideoValidator.class })
    public void update(){
        setVideo();
    }

    private void setVideo(){
        Video v = getModel(Video.class);
        v.set(Video.NAME, getPara("name"));
        v.set(Video.SORT, getPara("sort"));
        v.set(Video.ENABLED, getPara("enabled"));
        v.set(Video.URL, getPara("url"));
        v.set(Video.DETAIL, getPara("detail"));
        v.set(Video.REMARK, getPara("remark"));
        v.set(Video.IS_DELETED, false);

        String id = getPara(VIDEO_ID);
        if(StrKit.notBlank(id)){
            v.set(Video.ID, id);
            v.set(Video.LAST_UPDATED_TIME, new Date());
            int updator = InterfaceUtil.getAdminId();// 当前用户id
            v.set(Video.LAST_UPDATED_BY, updator);
            v.update();
        } else{
            int org_id = InterfaceUtil.getOrgId();// 当前用户机构id
            int creator = InterfaceUtil.getAdminId();// 当前用户id
            v.set(Video.CREATED_TIME, new Date());
            v.set(Video.CREATED_BY, creator);
            v.set(Video.ORG_ID, org_id);
            v.set(Video.LAST_UPDATED_TIME, new Date());
            v.set(Video.LAST_UPDATED_BY, creator);

            v.save();
        }

        redirect("/video/list");
    }

    /**
     * @Title: delete
     * @Description: 删除
     */
    @Before(Tx.class)
    public void delete() throws SQLException{
        softlyDeleteById("video");
    }

}
